package com.atguigu.controller;

import com.atguigu.entity.CartInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.CartInfoService;
import com.atguigu.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartInfoService cartInfoService;

    //1.商品添加到购物车
    @PostMapping("addToCart/{skuId}/{skuNum}")
    public RetVal addToCart(@PathVariable Long skuId, @PathVariable Integer skuNum, HttpServletRequest request) {
        String oneOfUserId = "";
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            oneOfUserId = AuthContextHolder.getUserTempId(request);
        } else {
            oneOfUserId = userId;
        }
        cartInfoService.addToCart(oneOfUserId, skuId, skuNum);
        return RetVal.ok();
    }

    //2.购物车列表信息
    @GetMapping("getCartList")
    public RetVal getCartList(HttpServletRequest request) {
        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartList = cartInfoService.getCartList(userId, userTempId);
        return RetVal.ok(cartList);
    }

    //3.商品的选中状态 http://api.gmall.com/cart/checkCart/37/1
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public RetVal checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked, HttpServletRequest request) {
        String oneOfUserId = "";
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            oneOfUserId = AuthContextHolder.getUserTempId(request);
        } else {
            oneOfUserId = userId;
        }
        cartInfoService.checkCart(oneOfUserId, skuId, isChecked);
        return RetVal.ok();
    }

    //4.购物车商品的删除 http://api.gmall.com/cart/deleteCart/25
    @DeleteMapping("deleteCart/{skuId}")
    public RetVal deleteCart(@PathVariable Long skuId, HttpServletRequest request) {
        String oneOfUserId = "";
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            oneOfUserId = AuthContextHolder.getUserTempId(request);
        } else {
            oneOfUserId = userId;
        }
        cartInfoService.deleteCart(skuId,oneOfUserId);
        return RetVal.ok();
    }

    //5.查询选中的商品信息---送货清单
    @DeleteMapping("getSelectedProduct/{userId}")
    public List<CartInfo> getSelectedProduct(@PathVariable String userId) {
        return cartInfoService.getSelectedProduct(userId);
    }

    //6.从数据库中查询最新的购物车信息到redis中
    @DeleteMapping("queryFromDbToRedis/{userId}")
    public List<CartInfo> queryFromDbToRedis(@PathVariable String userId) {
        return cartInfoService.queryFromDbToRedis(userId);
    }


}
